package bliffoscope.engine;


import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ddatta
 */
public class DataAnalyzer implements Runnable{
    private Subject testData       = null;
    private Subject searchTarget   = null;
    private List<Target> targetsFound   = null;
    private int tolerance = -1;
    private String type = null; 
    DataAnalyzer(Subject testData, Subject searchTarget, String type, int tolerance){
        this.searchTarget=searchTarget;
        this.testData = testData;
        this.type = type;
        this.tolerance = tolerance;
        this.targetsFound = new ArrayList<>();
    }
    
    @Override
    public void run() {
        System.out.println("running for .."+type);
        testData.trimColumns();
        searchTarget.trimColumns();       
        
        for(int row=0;row<=testData.getHeight()-searchTarget.getHeight();row++){  
            for(int col=0;col<=testData.getWidth()-searchTarget.getWidth();col++){
                //System.out.println("running for .."+type+" from ("+row+","+col+")");
                int totalBitMatch = 0;
                int[] bitMatch = new int[searchTarget.getHeight()];
                
                for(int k=0;k<searchTarget.getHeight();k++){
                    
                    //if(col==0){
                        //BitSet currB = (BitSet)testData.getBitSet().get(row+k).get(col, col+searchTarget.getWidth());
                        BitSet currB = (BitSet)testData.getBitSet().get(row+k).get(col, col+searchTarget.getBitSet().get(k).length());
                        //System.out.println(currB + "|"+searchTarget.getBitSet().get(k));
                        currB.and(searchTarget.getBitSet().get(k));
                        //System.out.println(currB + "|"+searchTarget.getBitSet().get(k));
                        bitMatch[k]=currB.cardinality();
                        totalBitMatch+=bitMatch[k];
//                    }
//                    else{
//                        
//                    }
                }
                
                float percent = totalBitMatch*100/searchTarget.getTotalBitCount();
                //System.out.println(percent);

                if(percent>tolerance){
                    //we might have a match
                    if(targetsFound==null)
                        targetsFound = new ArrayList<Target>();
                    targetsFound.add(new Target(type,col,row,col+searchTarget.getWidth(),row+searchTarget.getHeight(),percent));

                }
                
            }
        }
        //targetsFound.add(new Target(type,10,10,20,20,tolerance));  //dummy
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Subject getTestData() {
        return testData;
    }

    public void setTestData(Subject testData) {
        this.testData = testData;
    }

    public Subject getSearchTarget() {
        return searchTarget;
    }

    public void setSearchTarget(Subject searchTarget) {
        this.searchTarget = searchTarget;
    }

    public List<Target> getTargetsFound() {
        return targetsFound;
    }

    public void setTargetsFound(List<Target> targetsFound) {
        this.targetsFound = targetsFound;
    }
    
}
